import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/app/tools/date_util.dart';
import 'package:tin_flutter/ui/widget/common_product_vertical_widget.dart';

import '../../../../../app/res/app_color.dart';
import '../../../../../app/res/intl.dart';
import '../../main_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/11/4
/// * 时间: 16:42
/// * 描述: 秒杀专区
class SaleWidget extends StatefulWidget {
  const SaleWidget({Key? key}) : super(key: key);

  @override
  State<SaleWidget> createState() => _SaleWidgetState();
}

class _SaleWidgetState extends State<SaleWidget> {
  final state = Get.find<MainLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
      child: Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
        child: Padding(
          padding: EdgeInsets.all(12.0),
          child: Column(
            children: [
              // 标题栏
              Row(
                children: [
                  Image(image: AssetImage('assets/images/icon_flash_promotion.png'), height: 40.0),
                  SizedBox(width: 8.0),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(Intl().saleTitle, style: TextStyle(fontSize: 15.0)),
                      SizedBox(height: 1.0),
                      Text(
                          '${Intl().saleContentFirst} ${DateUtil.formatTimeStrToHms(state.flashPromotion.value?.nextStartTime)} ${Intl().saleContentLast}',
                          style: TextStyle(fontSize: 10.0, color: AppColor.grey500)),
                    ],
                  ),
                  Spacer(),
                  Column(
                    children: [
                      Text(Intl().saleEnd, style: TextStyle(fontSize: 10.0)),
                      SizedBox(height: 1.0),
                      Text('0:10:10', style: TextStyle(fontSize: 10.0)),
                    ],
                  )
                ],
              ),
              SizedBox(height: 5.0),
              // 列表
              GridView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  mainAxisSpacing: 5.0,
                  crossAxisSpacing: 5.0,
                  childAspectRatio: 0.7,
                ),
                itemCount: state.flashPromotion.value?.productList.length,
                itemBuilder: (context, index) {
                  final product = state.flashPromotion.value?.productList[index];
                  if (product == null) return SizedBox.shrink();
                  return CommonProductVerticalWidget(
                      imageUrl: product.pic,
                      title: product.name,
                      content: product.subTitle,
                      price: product.price.toInt());
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}
